import mta from './mta-wechat-analysis';

import { isHybrid, shareWeaApp, pageShow, pageHide } from './h5/hybrid';
import { parseQuery } from './common';

const name = 'https://avatar-qiniu.doctorwork.com/';

/**
 * 控制组件经过了hide 重新Show之后 执行某些生命周期的或者事件
 * @param {Array<String>|String} showSelfExecArray 重新调起componentDidShow周期执行的方法
 * @param {Array<String>|String} hideSelfExecArray 在componentDidHide周期执行的方法
 */
const rewriteDisplay = (
    showSelfExecArray,
    hideSelfExecArray
) => thisComponent =>
    class extends thisComponent {
        isHided = false;
        constructor(props) {
            super(props);
        }
        componentDidShow() {
            super.componentDidShow && super.componentDidShow();
            if (!this.isHided) {
                return;
            }
            if (typeof showSelfExecArray == 'string') {
                super[showSelfExecArray] && super[showSelfExecArray]();
            } else if (showSelfExecArray && isArray(showSelfExecArray)) {
                showSelfExecArray.forEach(functionName => {
                    super[functionName] && super[functionName]();
                });
            }
            // else{
            //     throw Error('showSelfExecArray must be an Array or a String,it belongs  component itselves')
            // }
            // console.log(self)
            this.isHided = false;
        }
        componentDidHide() {
            super.componentDidHide && super.componentDidHide();
            if (typeof hideSelfExecArray === 'string') {
                super[hideSelfExecArray] && super[hideSelfExecArray]();
            } else if (hideSelfExecArray && isArray(hideSelfExecArray)) {
                hideSelfExecArray.forEach(functionName => {
                    super[functionName] && super[functionName]();
                });
            }
            this.isHided = true;
        }
    };


// TODO: 给个页面设置分享的适配器
const pathAdapt = {
    productDetail: '/pages/productDetail/index'
};
/**
 * 重写打点和分享，基于h5的分享处理
 * @param {*} thisComponent
 */
const rewriteAddMTA = thisComponent =>
    class extends thisComponent {
        constructor(props) {
            super(props);
            // 注册componentDidShow事件
            if (isHybrid) {
                pageShow(() => {
                    this.componentDidShow();
                });
                pageHide(() => {
                    this.componentDidHide();
                });
            } else if (process.env.TARO_ENV == 'h5') {
                //h5 分享配置
                this.indexConfig = {
                    url: '/pages/index/index',
                    title: '健康好物年终大促底价集结',
                    desc: '企鹅医生健康商店',
                    imgUrl: `${name}o_1cujho50ubpkq9ige011bg1h50a.jpg`
                };
                this.otherShare = '/pages/tuanList/index/pages/grouping/index';
                let { title, path, imgUrl, desc } = this.indexConfig;
                this.props.dispatch({
                    type: 'wechat/setShare',
                    payload: {
                        title,
                        desc,
                        link: path,
                        imgUrl: imgUrl
                    }
                });
            }
        }
        componentWillMount() {
            super.componentWillMount && super.componentWillMount();
            mta.Page.init();
            // console.log('mta.Page.init');
        }
        h5configShape(res) {
            if (this.otherShare.indexOf(path) < -1) {
                //如果不是拼团之类的
                var { title, path, imageUrl = imgUrl, desc } = this.indexConfig;
            } else {
                var { title, path, imageUrl, desc='企鹅医生健康商店' } = super.onShareAppMessage(
                    res
                );
            }
            path = path.startsWith('/') ? path : `/${path}`;
            path = path
                ? location.href.indexOf('#') > -1
                    ? `${location.href.slice(
                        0,
                        location.href.indexOf('#')
                    )}#${path}`
                    : location.href
                : location.href;
            return {
                title,
                path,
                imageUrl,
                desc
            };
        }
        onShareAppMessage(res) {
            if (!super.onShareAppMessage) {
                return;
            }
            if (isHybrid) {
                const { title, path, imageUrl } = super.onShareAppMessage(res);
                shareWeaApp({
                    title,
                    path,
                    imageLink: imageUrl,
                    appId: 'gh_8a3cbf97c7ce'
                });
            } else if (process.env.TARO_ENV == 'weapp') {
                return super.onShareAppMessage(res);
            } else {
                //h5
                // title: '', // 分享标题
                // desc: '', // 分享描述
                // link: '', // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                // imgUrl: '', // 分享图标
                //1.判断路径是否第一个字符是包含斜杠,其他默认都是首页
                let { title, path, imageUrl, desc } = this.h5configShape(res);
                this.props.dispatch({
                    type: 'wechat/setShare',
                    payload: {
                        title,
                        desc,
                        link: path,
                        imgUrl: imageUrl,
                        callback: () => {
                            console.log('配置成功');
                            this.props.dispatch({
                                type: '$msg/showToast',
                                payload: {
                                    title: '点击右上角分享'
                                }
                            });
                        }
                    }
                });
            }
        }
    };

export { rewriteDisplay, rewriteAddMTA };

export const lifecycle = Comp => {
    const oldMount = Comp.prototype.componentWillMount;

    Comp.prototype.componentWillMount = function() {
        const { q } = this.$router.params;
        // 解析自定义参数
        if (q) {
            let params = parseQuery(decodeURIComponent(q));
            Object.assign(this.$router.params, params);
        }
        oldMount && oldMount.call(this);
    };
    return Comp;
};
